#include "sys.h"
#include "net.h"
#include "log.h"
#include "devinfo.h"

#define HG261GS_LOID_FILE "/var/loid.txt"

#define HG261GS_WAN_IFACE "pon0.41"

static int _inter_web_get(char *buf, char *name){
    FILE* fp;
    char buffer[128] = { 0 },  cmd[512] = {0};
    int ret = 0;

    if(access("/var/WEB-GUI/hgcxml.conf", F_OK) != 0)
        return 0;

    sprintf(cmd, ". /var/WEB-GUI/hgcxml.conf ; inter_web get $%s | cut -d'&' -f1", name);
    // fp = popen(". /var/WEB-GUI/hgcxml.conf ; inter_web get $IGD_DI_ModelName", "r");
    fp = popen(cmd, "r");

    fgets(buffer, sizeof(buffer), fp);
    buffer[strlen(buffer)-1] = 0;
    // log_trace("%s", buffer);
    pclose(fp);
    strncpy(buf, buffer, strlen(buffer));
    return 1;
}
int is_hg261gs(){
    int ret = 0;
    char buffer[128] = { 0 };

    if(0 == _inter_web_get(buffer, "IGD_DI_ModelName"))
        return ret;

    if (strstr(buffer, "HG261GS")) {
        ret = 1;
    }
    return ret;
}
int hg261gs_has_internet(){
    FILE *fp ;
    char buffer[128];
    int has_internet = 0;

    fp = popen("./hg261gs_pppoe.sh", "r");
    while(fgets(buffer, sizeof(buffer), fp) != NULL){
        if(strstr(buffer, "INTERNET_R"))
        {
            has_internet = 1;
            break;
        }
    }
    pclose(fp);
    return has_internet;
}
int hg261gs_is_bridge(){
    FILE *fp ;
    char buffer[128];
    int is_bridge = 0;

    fp = popen("./hg261gs_pppoe.sh", "r");
    while(fgets(buffer, sizeof(buffer), fp) != NULL){
        if(strstr(buffer, "INTERNET_B"))
        {
            is_bridge = 1;
            break;
        }
    }
    pclose(fp);
    return is_bridge;
}
int hg261gs_get_internet_r_pwd(char *usr, char *pwd){
    FILE *fp ;
    char buffer[128];

    fp = popen("./hg261gs_pppoe.sh get_route_pwd", "r");
    fgets(buffer, sizeof(buffer), fp);
    sscanf(buffer, "%s %s", usr, pwd);
    pclose(fp);
    if(strlen(usr) ==0 || strlen(pwd) == 0)
        return 0;
    return 1;
}
void hg261gs_get_mac(char *mac){
    getifaddr(HG261GS_WAN_IFACE, mac, "mac");
}

static void _get_loid_info(char *loid, char *loidKey){
    FILE *fp;
    char buf[128] = {0};
    char *delim = "=", *ptr, *saveptr = NULL;

    fp = fopen(HG261GS_LOID_FILE, "r");
    if(!fp){
        log_syserr("can not open "HG261GS_LOID_FILE);
        return ;
    }
    fgets(buf, sizeof(buf), fp);
    buf[strlen(buf) - 1] = 0;
    ptr = strtok_r(buf, delim, &saveptr);
    ptr = strtok_r(NULL, delim, &saveptr);
    strcpy(loid, ptr);

    fgets(buf, sizeof(buf), fp);
    buf[strlen(buf) - 1] = 0;
    ptr = strtok_r(buf, delim, &saveptr);
    ptr = strtok_r(NULL, delim, &saveptr);
    strcpy(loidKey, ptr);

    fclose(fp);
}

void hg261gs_enable_mirror(DevInfo_t* devinfo){
    if(strstr(devinfo->sw_version, "G60D1")){
        system("mirror set 0001000000000 0001000000000 12");
        log_trace("hg261gs mirror cmd: mirror set 0001000000000 0001000000000 12");
    }else if(strstr(devinfo->sw_version, "G50D1")){
        system("mirror set 5 6 ingress enable && mirror set 5 6 egress enable");
        log_trace("hg261gs mirror cmd: mirror set 5 6 ingress enable && mirror set 5 6 egress enable");
    }
}
void hg261gs_disable_mirror(DevInfo_t* devinfo){
    if(strstr(devinfo->sw_version, "G60D1")){
        system("mirror set 0 0 0");
        log_trace("hg261gs mirror cmd: mirror set 0 0 0");
    }else if(strstr(devinfo->sw_version, "G50D1")){
        system("mirror set 5 6 ingress disable && mirror set 5 6 egress disable");
        log_trace("hg261gs mirror cmd: mirror set 5 6 ingress disable && mirror set 5 6 egress disable");
    }
}

void hg261gs_get_devinfo(DevInfo_t* devinfo){
    char buffer[128] = {0}, SN[32] = {0}, OUI[32] = {0};
    strcpy(devinfo->factory,"FiberHome");

    _inter_web_get(buffer, "IGD_DI_ModelName");
    strcpy(devinfo->model, buffer);

    _inter_web_get(buffer, "IGD_DI_SoftwareVersion");
    strcpy(devinfo->sw_version, buffer);

    _inter_web_get(OUI, "IGD_DI_ManufacturerOUI");
    _inter_web_get(SN, "IGD_DI_SerialNumber");
    bzero(buffer, sizeof(buffer));
    strcat(buffer, OUI);
    strcat(buffer, "-");
    strcat(buffer, SN);
    strcpy(devinfo->gatewaySN, buffer);

    _get_loid_info(devinfo->loid, devinfo->loidKey);

    get_os_version(devinfo->osVersion);

    strcpy(devinfo->osType, "Linux");
    strcpy(devinfo->dev_type, "ONT");
    devinfo->clientPattern = hg261gs_is_bridge() ? 2 : 1;// 1:router 2:bridge
    devinfo->flashSize = 0;
    devinfo->needUpdateSysTime = 1;

}